/**
 * 管理初始化
 */
var Policy = {
    id: "PolicyTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Policy.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '保险公司简称', field: 'insuranceAbbr', visible: true, align: 'center', valign: 'middle'},
        {title: '保单号', field: 'policy_code', visible: true, align: 'center', valign: 'middle'},
        {title: '保单类型', field: 'policyTypeName', visible: true, align: 'center', valign: 'middle'},
        {title: '所属客户', field: 'tenantName', visible: true, align: 'center', valign: 'middle'},
        {title: '生效日期', field: 'effective_date', visible: true, align: 'center', valign: 'middle',
            formatter: function (value) {
                return Feng.dateFormat(value,"yyyy-MM-dd");
            }},
        {title: '失效日期', field: 'invalid_date', visible: true, align: 'center', valign: 'middle',
            formatter: function (value) {
                return Feng.dateFormat(value,"yyyy-MM-dd");
            }},
        {title: '缴费周期', field: 'paymentCycleName', visible: true, align: 'center', valign: 'middle'},
        {title: '计费方式', field: 'chargeModeName', visible: true, align: 'center', valign: 'middle'},
        {title: '计费截至日', field: 'deadline', visible: true, align: 'center', valign: 'middle'},
        {title: '保单状态', field: 'policyStautsName', visible: true, align: 'center', valign: 'middle'},
        {title: '附件数量', field: 'accessory_num', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Policy.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Policy.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
Policy.openAddPolicy = function () {
    var index = layer.open({
        type: 2,
        title: '添加保单',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/policy/policy_add'
    });
    this.layerIndex = index;
};
/**
 * 保存联系人
 */
Policy.openAddContacts = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '保存联系人',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policy/contacts_add/' + Policy.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 设置险种
 */
Policy.openInsuranceKind = function () {
    if (this.check()) {
        window.location = Feng.ctxPath + "/insuranceKind/" + Policy.seItem.id;
    }
};
/**
 * 设置保障计划设置
 */
Policy.openPolicyPlanKind = function () {
    if (this.check()) {
        window.location = Feng.ctxPath + "/policyPlan/" + Policy.seItem.id;
    }
};
/**
 * 附件管理
 */
Policy.openAccessory = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '附件管理',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/uploadFile/accessory/policy/' + Policy.seItem.id + '/50'
        });
        this.layerIndex = index;
    }
};

/**
 * 保单缴费
 */
Policy.openPolicyPay = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '保单缴费附件管理',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policy/pay/' + Policy.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 保单续签
 */
Policy.openPolicyReNew = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '保单续签',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policy/re_new/' + Policy.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看详情
 */
Policy.openPolicyDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改保单',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/policy/policy_update/' + Policy.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
Policy.delete = function () {
    if (this.check()) {
        Feng.confirm("是否删除保单 "+this.seItem.policy_code+"（"+ this.seItem.insuranceAbbr +"）?",function () {
            var ajax = new $ax(Feng.ctxPath + "/policy/delete", function (data) {
                Feng.success("删除成功!");
                Policy.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("policyId",Policy.seItem.id);
            ajax.start();
        })
    }
};

/**
 * 查询列表
 */
Policy.search = function () {
    Policy.table.refresh({query: Policy.formParams()});
};
/**
 * 查询列表
 */
Policy.resetSearch = function () {
    $("#policyCode").val("");
    $("#policyStauts").val(0);

    Policy.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Policy.formParams = function() {
    var queryData = {};

    queryData['policyCode'] = $("#policyCode").val();
    queryData['policyStauts'] = $("#policyStauts").val();

    return queryData;
}

$(function () {
    var defaultColunms = Policy.initColumn();
    var table = new BSTable(Policy.id, "/policy/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Policy.formParams());
    Policy.table = table.init();
});
